package com.jltx.xq.simplewatch.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.BaseAppCompatActivity;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;
import com.jltx.xq.simplewatch.base.presenter.BasePresenter;
import com.jltx.xq.simplewatch.entity.event.MainActivityDrawerSwitchEvent;
import com.jltx.xq.simplewatch.entity.event.NewsFragmentSwitchEvent;
import com.jltx.xq.simplewatch.news.ui.TopicNBANewsFragment;
import com.jltx.xq.simplewatch.news.ui.TopicNewsFragment;
import com.jltx.xq.simplewatch.news.ui.WeChatNewsFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class MainActivity extends BaseAppCompatActivity {
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    @BindView(R.id.drawer_act_main)
    DrawerLayout drawerLayout;
    @BindView(R.id.nav_act_main)
    NavigationView navigationView;
    @BindView(R.id.fragment_cotainer)
    FrameLayout cotentFrameLayout;

    private int defaultPage = 0;
    private List<Integer> navMenuId = new ArrayList<>();
    public static final String NAV_DEFAULT_INDEX = "nav_default_index";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        if(savedInstanceState == null){
            loadRootFragment(R.id.fragment_cotainer,MainFragment.newInstance());
        }else {
            defaultPage = savedInstanceState.getInt(NAV_DEFAULT_INDEX);
        }

        navMenuId.clear();
        navMenuId.add(R.id.nav_wechat_act_main);
        navMenuId.add(R.id.nav_toutiao_act_main);
        navMenuId.add(R.id.nav_nba_act_main);

        navigationView.setNavigationItemSelectedListener(navItemSelectedListener);
        navigationView.setCheckedItem(navMenuId.get(defaultPage));
    }

    private NavigationView.OnNavigationItemSelectedListener navItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int lastDefaultPage = defaultPage;
            int index = navMenuId.indexOf(item.getItemId());
            BaseSupportFragment newFragment = null;
            if(index == -1){
                index = 0;
            }

            switch (index){
                case 0:
                    newFragment = WeChatNewsFragment.newInstance(getString(R.string.wechat_code));
                    break;
                case 1:
                    newFragment = TopicNewsFragment.newInstance();
                    break;
                case 2:
                    newFragment = TopicNBANewsFragment.newInstance();
                    break;
                default:
                    break;
            }
            defaultPage = index;

            if(lastDefaultPage != defaultPage && newFragment != null){
                postNewsFragmentSwitchEvent(newFragment);
            }
            drawerLayout.closeDrawers();
            return true;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        registerEventBus(MainActivity.this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterEventBus(MainActivity.this);
    }

    @Override
    public void onBackPressedSupport() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        if(drawerLayout.isDrawerOpen(navigationView)){
            drawerLayout.closeDrawers();
        }else if(isRootInTop()){
            super.onBackPressedSupport();
        }else {
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                super.onBackPressedSupport();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                Snackbar snackbar = Snackbar.make(cotentFrameLayout,getString(R.string.press_again_exit_act_main), Snackbar.LENGTH_SHORT);
                View view = snackbar.getView();
                if(view!=null){
                    view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                }
                snackbar.show();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
    /**
     * 发送切换新闻界面的事件
     * @param supportFragment
     */
    private void postNewsFragmentSwitchEvent(BaseSupportFragment supportFragment){
        NewsFragmentSwitchEvent event = new NewsFragmentSwitchEvent();
        event.setNewFragment(supportFragment);
        EventBus.getDefault().post(event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void switchDrawerLayout(MainActivityDrawerSwitchEvent event){
        if(event != null && drawerLayout != null){
            if(event.isEnable()){
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
            }else {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

        }
    }

}
